﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Taurus.Platform.XML;
using Taurus.Parking.DTO;
using Taurus.Parking.XML;
using Taurus.Platform.BLL;
using System.Threading;
using Taurus.Platform.DTO;

namespace Taurus.Parking.BLL
{
    class InitSys
    {
        #region field
        private static InitSys _is;

        /// <summary>
        /// MyGarage集合
        /// </summary>
        private Dictionary<string, MyGarage> _garage;
        private Dictionary<string, ManualResetEvent> _manualResetEvent;

        private IPersistence<Config> _configP;
        private IPersistence<ParkingUser> _parkingUserP;
        /// <summary>
        /// 账号逻辑
        /// </summary>
        private AccountBll _ab;
        #endregion

        #region construction

        private InitSys() { }

        #endregion

        public void Init()
        {
            _garage = new Dictionary<string, MyGarage>();
            _configP = new ConfigPersistence();
            _parkingUserP = new ParkingUserPersistence();
            _ab = new AccountBll();
            _manualResetEvent = new Dictionary<string, ManualResetEvent>();

            InitConfig();
            foreach (var ua in _ab.GetAllAccount())
            {
                _manualResetEvent.Add(ua.UID, new ManualResetEvent(false));
                ThreadPool.QueueUserWorkItem(new WaitCallback(InitMyGarage), ua);
            }
            //等待线程完成初始化任务
            WaitHandle.WaitAll(_manualResetEvent.Values.ToArray<ManualResetEvent>());
        }

        #region property

        public static InitSys Instance
        {
            get
            {
                if (_is == null)
                {
                    _is = new InitSys();
                }
                return _is;
            }
        }

        public List<MyGarage> MyGarageList
        {
            get
            {
                return _garage.Values.ToList<MyGarage>();
            }
        }

        #endregion

        #region public method

        public MyGarage GetMyGarage(string uid)
        {
            return _garage[uid];
        }

        #endregion

        #region private method

        /// <summary>
        /// 初始化系统配置
        /// </summary>
        private void InitConfig()
        {
            foreach (var ua in _ab.GetAllAccount())
            {
                if (_configP.GetObject(ua.UID) == null)
                {
                    Config cf = new Config(ua.UID);//默认配置
                    _configP.Insert(cf);
                }
            }
            _configP.Save();
        }
        /// <summary>
        /// 由多线程同步初始化数据
        /// </summary>
        private void InitMyGarage(object userAccount)
        {
            UserAccount ua = (UserAccount)userAccount;
            ParkingUser pu = _parkingUserP.GetObject(ua.UID);
            if (pu == null)
            {
                pu = new ParkingUser(ua);
            }
            MyGarage mg = new MyGarage(pu);
            _garage.Add(pu.UID, mg);
            _manualResetEvent[ua.UID].Set();
        }

        #endregion
    }
}
